import static org.junit.Assert.*;
import java.util.EmptyStackException;
import java.util.Iterator;
import org.junit.Test;


public class ArrayStackTest {

	@Test
	public void testSize() throws Exception{
		ArrayStack stack=new ArrayStack();
		assertEquals(0, stack.size());
		stack.push(null);
		stack.push(34);
		stack.push(null);
		assertEquals(3, stack.size());
		stack.pop();
		assertEquals(2, stack.size());
	}

	@Test
	public void testIsEmpty() throws Exception{
		ArrayStack stack=new ArrayStack();
		assertTrue(stack.isEmpty());
		stack.push(null);
		assertFalse(stack.isEmpty());
	}

	@Test
	public void testClear() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.push(null);
		stack.push(7);
		stack.push(null);
		stack.push(null);
		assertFalse(stack.isEmpty());
		stack.clear();
		assertTrue(stack.isEmpty());
	}

	@Test(expected = EmptyStackException.class)
	public void testPeekEmpty() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.peek();
		
	}
	
	@Test
	public void testPeek() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.push(null);
		stack.push(3);
		assertEquals(3, (int) stack.peek());
		stack.pop();
		assertNull(stack.peek());		
	}

	@Test(expected = EmptyStackException.class)
	public void testPopEmpty() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.pop();
	}

	@Test
	public void testPop() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.push(null);
		stack.push(3);
		assertEquals(3, (int) stack.pop());
		assertNull(stack.pop());
				
	}

	@Test
	public void testPush() throws Exception{
		ArrayStack stack=new ArrayStack();
		assertEquals(4, (int) stack.push(4));
		assertEquals(4, (int) stack.peek());
	}

	@Test
	public void testSearch() throws Exception{
		ArrayStack stack=new ArrayStack();
		assertEquals(-1, stack.search(null));
		stack.push(4);
		assertEquals(0, stack.search(4));
	}

	@Test
	public void testToString() throws Exception{
		ArrayStack stack=new ArrayStack();
		stack.push(4);
		stack.push(3);
		assertTrue("[4, 3]".equals(stack.toString()));
	}
	
	@Test
    public void testIterator() throws Exception {
        ArrayStack stack = new ArrayStack();
        Iterator<Integer> it=stack.iterator();
        assertFalse(it.hasNext());
        stack.push(null);
        stack.push(4);
        assertTrue(it.hasNext());
        assertNull(it.next());
    }
	
	@Test(expected = UnsupportedOperationException.class)
    public void testIteratorRemove() throws Exception {
        ArrayStack stack = new ArrayStack();
        Iterator<Integer> it=stack.iterator();
        it.remove();
    }

}
